<?php
class ManageArticlesController extends Controller {
	private $_model;
	public $layout='//layouts/main';
	public $pageTitle="Manage tutorials";

	/*
	public function allowedActions() {
			return '';
	}
	
	/*
	public function accessRules() {
			return array(
					array('deny',
							'actions'=>array('delete', 'create', 'update'),
							'users'=>array('?'),
					),
					array('deny',
							'actions'=>array('delete', 'create', 'update'),
							'users'=>array('guest'),
					),
					array('allow',
							'actions'=>array('admin'),
							'roles'=>array('guest'),
					),
					array('allow',
							'actions'=>array('delete', 'create', 'update'),
							'roles'=>array('superuser'),
					),
			);
			//return '';
	}
	*/
	public function init()
	{
			Yii::app()->theme = 'backend';
			parent::init();
	}

	public function actionView($id) {
		$this->pageTitle = 'View an article';
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Article'),
		));
	}

	public function actionCreate() {
		$model = new Article;
		if (isset($_POST['Article'])) {
			$model->setAttributes($_POST['Article']);
			$model->user_id = Yii::app()->user->id;
			$model->country_id = Country::model()->visitorCountryID();
			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		} else {
			$model->is_published = 1;
		}

		$this->pageTitle = 'Create an tutorials';
		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'Article');

		if (isset($_POST['Article'])) {
			$model->setAttributes($_POST['Article']);
			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->pageTitle = 'Update an article';
		$this->render('update', array(
			'model' => $model,
		));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			//$this->loadModel($id, 'Article')->delete();
			$model = $this->loadModel($id);
			
			// X�a khoi db
			if ($model->delete() == 'ALL') 
			{
				// X�a image
				$uploadPath = YiiBase::getPathOfAlias('webroot') . Yii::app()->controller->module->image['url'];
				$filename = $uploadPath . $model->image;
				if (is_file($filename)) @unlink($filename);
				$filenameThumb = $uploadPath . 'thumb_' . $model->image;
				if (is_file($filenameThumb)) @unlink($filenameThumb);
				$filenameLager = $uploadPath . 'lager_' . $model->image;
				if (is_file($filenameLager)) @unlink($filenameLager);
			}
			
			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Article');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		if (isset($_POST['article-grid_c0'])) {
			foreach ($_POST['article-grid_c0'] as $ID)
			{
				$model = $this->loadModelByID($ID);

				// Xoa khoi db
				if ($model->delete() == 'ALL')
				{
					
				}
			}
		}
		$model = new Article('search');
		$model->unsetAttributes();
		if (isset($_GET['Article'])) {
			$model->setAttributes($_GET['Article']);
		}
		$this->render('admin', array(
			'model' => $model,
		));
	}
	
	protected function performAjaxValidation($models)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='article-form')
		{
			echo CActiveForm::validate($models);
			Yii::app()->end();
		}
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=Article::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
	
	public function loadModelByID($id)
	{
		$model = Article::model()->findByPk($id);
		if ($model === null)
				throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}
	
	public function actionUpload(){
		Yii::import('ext.ckeditor.CkEditorUploader');
		$config = array(
				'url'=>$this->module->image['url'],
		);
		CkEditorUploader::$config = $config;
		CkEditorUploader::upload();
	}
}
